// pages/goods/goodsDetails/goodsDetails.js
const app = getApp()
var help = require('../../../utils/help.js');
var login = require('../../../utils/login.js');
var WxParse = require('../../../wxParse/wxParse.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageUrl: app.data.imageUrl,
    data: {},
    id: 0,

    imgUrls: [], // 幻灯片
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 500,

    //模态框
    showOneButtonDialog: false,
    showTwoButtonDialog: false,
    num:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadData();
  },

  //模态框代码
  tapDialogButton(e) {
    this.setData({
      num: 1,
      showOneButtonDialog: false,
      showTwoButtonDialog: false
    })
  },
  addCart(e) {
    var that = this;
    this.setData({
      showOneButtonDialog: true
    })
  },
  jian: function () {
    if (this.data.num > 1) {
      this.setData({
        num: this.data.num - 1
      })
    }
  },
  jia: function () {
    if (this.data.num < this.data.data.kucun) {
      this.setData({
        num: this.data.num + 1
      })
    }
  },
  set_num: function (e) {
    var num = e.detail.value;
    if (num <= 1) {
      num = 1;
    } else if (num >= this.data.data.kucun) {
      num = this.data.data.kucun;
    }
    this.setData({
      num: num
    })
  },
  addCart(e) {
    var action = e.currentTarget.dataset.id;
    var that = this;
    //幻灯片
    help.get(app.data.apiUrl + 'Goods/addCard', {
      token: app.data.userInfo ? app.data.userInfo.token : '',
      goods_id: that.data.data.id,
      num: that.data.num
    }, that, function (res, that) {
      var result = res.data;
      // console.log(result);
      if (result.code == 1) {
        if (action == 0) {
          help.msg('加入购物车成功');
        } else {
          help.goUrlTab('/pages/shopcart/cart/cart');
        }
      } else {
        help.msg(result.msg)
      }

    });


  },
  closePopup: function (e) {
    this.setData({
      showTwoButtonDialog: false
    })
  },

  goIndex: function(){
    help.goUrlTab('/pages/index/index');
  },

  /**
   * 加载数据
   */
  loadData: function () {
    var that = this;
    var url = app.data.apiUrl + 'Goods/info';
    var data = {
      token: app.data.userInfo ? app.data.userInfo.token : '',
      id: that.data.id
    };
    help.get(url, data, that, function (res, that) {
      var result = res.data;
      // console.log(result);
      if (result.code == 1) {
        that.setData({
          data: result.data,
          imgUrls: result.data.product_images,
        })
        wx.setNavigationBarTitle({
          title: result.data.name
        })
        WxParse.wxParse('content', 'html', result.data.content, that, 5);
      } else if (result.code == 1) {
        help.msg('登录已失效，正在重新登录...')
        // 重新检测登录状态
        login.getUserInfo(app);
      } else {
        help.msg(result.msg)
        return false;
      }

    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})